package com.example.klaraservice.controller;

import com.example.klaraservice.pojo.*;
import com.example.klaraservice.service.CartService;
import com.example.klaraservice.service.CommodityService;
import com.example.klaraservice.service.OrderItemService;
import com.example.klaraservice.service.OrdersService;
import com.example.klaraservice.utils.UrlUtil;
import com.example.klaraservice.vo.R;
import io.swagger.annotations.Api;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : Glacialerisa
 * @date : 2023/6/13 22:51
 * @desc :
 */
@Api(tags = "订单")
@RestController
@RequestMapping("/api/orders")
public class OrdersController {
    @Resource
    private OrdersService ordersService;
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private CartService cartService;
    @Resource
    private CommodityService commodityService;
    @PostMapping("/addOrders")
    public R addOrders(@RequestBody Orders orders, @RequestParam int userId){
        boolean result = ordersService.addOrder(orders, userId);
        int ordersId = orders.getId();
        if (result){
            return R.ok().data("ordersId",ordersId);
        }else {
            return R.error();
        }
    }
    @GetMapping("/getOrdersByOrdersNumber")
    public R getOrdersByOrdersNumber(@RequestParam String ordersNumber){
        Orders data = ordersService.selectByOrdersNumber(ordersNumber);
        if (data!=null){
            return R.ok().data("data", data);
        }
        return R.error();
    }
    @GetMapping("/getUserOrders")
    public R getUserOrders(@RequestParam int userId){
        List<Orders> data = ordersService.getUserOrders(userId);
        if (data!=null){
            return R.ok().data("data", data);
        }
        return R.error();
    }
    @Transactional
    @PostMapping("/orderSubmit")
//    public R orderSubmit(@RequestBody Orders orders,
//                         @RequestParam String id,
//                         @RequestBody List<OrderItem> orderItemList,
//                         @RequestBody List<Cart> cartList)
    public R orderSubmit(@RequestBody OrderSubmitUtils orderSubmitUtils){
        Orders orders = orderSubmitUtils.getOrders();
        User user = orderSubmitUtils.getUser();
        List<OrderItem> orderItemList= orderSubmitUtils.getOrderItemList();
        List<Cart> cartList= orderSubmitUtils.getCartList();
//        int uid = Integer.parseInt(id);
//        System.out.println(orderSubmitUtils.getUser().getId()+"-----------------");
//        System.out.println(orderSubmitUtils.getOrders().toString()+"-----------------");
//        if (orders!=null&&uid!=0&&orderItemList!=null&&cartList!=null){
        if (orders!=null&&
                user.getId()!=0&&
                orderItemList!=null&&
                cartList!=null){
            ordersService.addOrder(orders, user.getId());
//            System.out.println(orders.getOrdersNumber()+"------------------------orderNumber");
            for (OrderItem oi:orderItemList
            ) {
                //修改图片url
                String image = oi.getImage();
                String newUrl = UrlUtil.getUrl(image);
                oi.setImage(newUrl);
                orderItemService.addOrderItem(oi, orders.getId());
            }
            for (Cart c:cartList
            ) {
                int stock = c.getCommodity().getStock();
                stock-=c.getNumber();
                c.getCommodity().setStock(stock);
                commodityService.updateCommodityInfo(c.getCommodity());
                cartService.deleteCartById(c.getId());
            }
            return R.ok().data("ordersNumber",orders.getOrdersNumber());
        }
        return R.error();
    }
}
